<?php
/*
 * Smarty plugin
 * -------------------------------------------------------------
 * File:     modifier.cpfcnpj.php
 * Type:     modifier
 * Name:     cpfcnpj
 * Purpose:  formata um número como CPF ou CNPJ
 * -------------------------------------------------------------
 */
function smarty_modifier_cpfcnpj($string)
{
    if(strlen($string)==11) { 
        return format_string('999.999.999-99', $string, '9');
    } else if (strlen($string)==14) {
        return format_string('99.999.999/9999-99', $string, '9');
    } else {
        return $string;
    }
}

function format_string($mask, $contents, $ch = '#') {

     $c = 0;
     $str = '';

     for($m = 0; $m < strlen($mask); $m++)
         if ($mask[$m] == $ch) {
             $str .= $contents[$c];
             $c++;
         }
         else
             $str .= $mask[$m];

     return($str);
}
?>